//
//  MemberSecondViewController.m
//  YiqiMobile
//
//  Created by henry on 13-4-19.
//  Copyright (c) 2013年 henry. All rights reserved.
//

#define  MEMBERSECONDBUTTONTAD 400
#import "MemberSecondViewController.h"
#import "AppDelegate.h"
#import "Utility.h"
#import "Config.h"
#import "HomePageViewController.h"
#import "MemberSecondCell.h"
#import "PersonalViewController.h"
#import "JSON.h"
#import "MBProgressHUD.h"
#import "DefineAction.h"
#import "Config.h"
#import "Utility.h"

@implementation MemberSecondViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    //导航栏左边按钮
    self.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc] initWithCustomView:[Utility customButton:self action:@selector(onClickAction:) normalImage:@"pushButton_normal.png" lightedImage:@"pushButton_hover.png" frame:CGRectMake(0, 0, 35, 35) title:nil tag:PUSHMENUBUTTONTAG]];
    //导航栏右边按钮
    self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc] initWithCustomView:[Utility customButton:self action:@selector(onClickAction:) normalImage:@"moreButton_normal.png" lightedImage:@"moreButton_hover.png" frame:CGRectMake(0, 0, 35, 35) title:nil tag:PUSHMENUBUTTONTAG+1]];
    
    float tableHeight=self.view.frame.size.height-self.navigationController.navigationBar.frame.size.height-49-48;
    CGRect tableViewFrame=CGRectMake(0, 48, UISCREEN_WIDTH, tableHeight);
    self.mytableView=[[UITableView alloc] initWithFrame:tableViewFrame style:UITableViewStylePlain];
    self.mytableView.separatorStyle=UITableViewCellSeparatorStyleSingleLine;
    //self.mytableView.separatorColor=[UIColor colorWithRed:249.0/255.0 green:249.0/255.0 blue:249.0/255.0 alpha:1.0];
    self.mytableView.delegate=self;
    self.mytableView.dataSource=self;
    [self.view addSubview:self.mytableView];
     self.arrayList=[NSMutableArray array];
    [self.mytableView reloadData];

    [self showSubMenu:48];
}

-(void) showSubMenu:(float)height{
     UIButton *chenyuanButton=[Utility customButton:self action:@selector(onclickSubMenu:) normalImage:@"qz_cy_normal.png" lightedImage:@"qz_cy_hover.png" frame:CGRectMake(0, 0, 107, height) title:nil tag:MEMBERSECONDBUTTONTAD];
    //[chenyuanButton addSubview:[self customLabel:@"38"]];
    UIButton *readerButton=[Utility customButton:self action:@selector(onclickSubMenu:) normalImage:@"qz_dz_normal.png" lightedImage:@"qz_dz_hover.png" frame:CGRectMake(107, 0, 106, height) title:nil tag:MEMBERSECONDBUTTONTAD+1];
    [readerButton addSubview:[self customLabel:@"39"]];
    UIButton *yaoqingButton=[Utility customButton:self action:@selector(onclickSubMenu:) normalImage:@"qz_yaoqing_normal.png" lightedImage:@"qz_yaoqing_hover.png" frame:CGRectMake(213, 0, 107, height) title:nil tag:MEMBERSECONDBUTTONTAD+2];
    
    [self.view addSubview:chenyuanButton];
    [self.view addSubview:readerButton];
    [self.view addSubview:yaoqingButton];
}
-(void) viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    AppDelegate *app=(AppDelegate*)[[UIApplication sharedApplication] delegate];
    self.hidesBottomBarWhenPushed=NO;
    [app.tabBarController hideExistingTabBar];
    app.tabBarController.tabBarView.hidden=NO;
    HomePageViewController *homePageViewController=(HomePageViewController*)[[[self.tabBarController.viewControllers objectAtIndex:0] viewControllers] objectAtIndex:0];
    homePageViewController.isShowLevelPop=YES;
    UIView *rightPopView=[self.view  viewWithTag:HOMEPAGELPV];
    [rightPopView removeFromSuperview];
    

}

-(IBAction)onClickAction:(id)sender{//导航顶部按钮
    HomePageViewController *homepage=(HomePageViewController*)[[[self.tabBarController.viewControllers objectAtIndex:0] viewControllers] objectAtIndex:0];
    [homepage onClickAction:sender];
}

- (IBAction)onclickSubMenu:(id)sender{//顶部切换菜单
    UIButton *selectButton=(UIButton*)sender;
    for (int i=0; i<3; i++) {
        UIButton *button=(UIButton*)[self.view viewWithTag:MEMBERSECONDBUTTONTAD+i];
        button.selected=NO;
    }
    selectButton.selected=YES;
    switch (selectButton.tag-MEMBERSECONDBUTTONTAD) {
        case 0:{//成员
            
            break;
        }
        case 1:{//读者
            
            break;
        }
        case 2:{//邀请好友
            
            break;
        }
            
    }

}



//表视图委托
#pragma mark -
#pragma mark table view data source methods

//返回某个表视图有多少行数据
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [self.arrayList count];
}

//设置行高度
- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 68;
}


//表视图显示表视图项时调用：第一次显示（根据视图大小显示多少个视图项就调用多少次）以及拖动时调用
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    static NSString *CellIdentifier = @"memberSecondCell";
    
    MemberSecondCell *cell =(MemberSecondCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[MemberSecondCell alloc] initWithStyle:UITableViewCellStyleDefault
                                         reuseIdentifier: CellIdentifier];
    }
    NSDictionary *dict=[self.arrayList objectAtIndex:indexPath.row];
    cell.logo.imageUrl=[NSString stringWithFormat:@"%@/%@?id=%@&type=3",[Utility  getServerUrl],DOWNFILEACTION,[dict objectForKey:@"u0"]];
    cell.content.text=[dict objectForKey:@"u2"];
    cell.detailTitle.text=[NSString stringWithFormat:@"供稿：%@   评论：%@    博文：%@",[dict objectForKey:@"count6"],[dict objectForKey:@"count4"],[dict objectForKey:@"count0"]];
    return cell;
}



//某行已经被选中时调用
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    // [tableView deselectRowAtIndexPath:indexPath animated:YES];//在弹出警告后自动取消选中表视图单元
    PersonalViewController *personalViewController=[[PersonalViewController alloc] initWithNibName:nil bundle:nil];
    [personalViewController showData:nil];
    AppDelegate *app=(AppDelegate*)[[UIApplication sharedApplication] delegate];
    personalViewController.hidesBottomBarWhenPushed=YES;
    [app.tabBarController hideExistingTabBar];
    app.tabBarController.tabBarView.hidden=YES;
    [self.navigationController pushViewController:personalViewController animated:YES];
    
}



-(void) loadData:(NSDictionary*) param{
      self.navigationItem.titleView=[Utility getTitleView:[param objectForKey:@"str2"]];
    AppDelegate *app=(AppDelegate*)[[UIApplication sharedApplication] delegate];
    if (app.status==NotReachable) {
        [Utility customAlertWithMsg:NETWORTERROE andTitle:ALRRTTITLE andDelagate:nil];
        return;
    }
    NSString *url=[NSString stringWithFormat:@"%@/%@",[Utility  getServerUrl],GETUBYE];
    NSMutableDictionary *options=[NSMutableDictionary dictionary];
    [options setObject:[param objectForKey:@"str1"] forKey:@"u.u0"];
    [options setObject:[[NSUserDefaults standardUserDefaults] objectForKey:USERID] forKey:@"e.e0"];
    [options setObject:[NSString stringWithFormat:@"%d",1] forKey:@"jr.curPageNum"];
    [self.arrayList removeAllObjects];
    self.request=[[URLRequest alloc] initWithPostURL:url andDelegate:self andPostData:options andFile:nil];
    [[MBProgressHUD showHUDAddedTo:self.view animated:YES] setLabelText:LOADING_TITLE_DEFAULT];
    [self.request startLoadData];
    
}
- (void)requestFinished:(id)context{
    [MBProgressHUD hideAllHUDsForView:self.view animated:NO];
    @try{
        NSDictionary *result=[context JSONValue];
        if (result!=nil&&[[result objectForKey:@"code"] integerValue]==0) {
            if ([[result objectForKey:@"method"] isEqualToString:GETUBYE]) {
                UIButton *chenyuanButton=(UIButton*)[self.view viewWithTag:MEMBERSECONDBUTTONTAD];
                [chenyuanButton addSubview:[self customLabel:[NSString stringWithFormat:@"%d",self.arrayList.count]]];
                [self.arrayList addObjectsFromArray:[result objectForKey:@"list"]];
                [self.mytableView reloadData];
           
            }
            
        }else{
            [Utility customAlertWithMsg:PROCESSERROE andTitle:ALRRTTITLE andDelagate:nil];
        }
    }@catch (NSException *e) {
        [Utility customAlertWithMsg:NETWORTERROE andTitle:ALRRTTITLE andDelagate:nil];
    }
}

- (void)requestFailed:(id)context error:(NSString *)error{
    [Utility customAlertWithMsg:NETWORTERROE andTitle:ALRRTTITLE andDelagate:nil];
}



-(UILabel *) customLabel:(NSString *) text{
    UILabel *label= [Utility detailTextLabel:CGRectMake(0, 25, 106, 15) andFontSize:12];
    label.textAlignment=NSTextAlignmentCenter;
    label.tag=1000;
    label.textColor=UIColorFromRGB(0x000000);
    label.text=text;
    return label;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
